﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mimoza.Transport;
using System.IO;
using System.Reflection;
using Mimoza.Common;

namespace Mimoza.ServiceLayer
{
    /// <summary>
    /// Create and configure all objects.
    /// </summary>
    public class Configuration
    {
        public string c_DBName = "Service.sdf";

        public Configuration()
        {
        }

        public Settings Settings
        {
            get { return m_settings; }
        }

        public void Build()
        {
            m_settings = new Settings();

            Mimoza.DBLayer.SQLSC.SQLSC_DBLayer dbLayer = 
                new Mimoza.DBLayer.SQLSC.SQLSC_DBLayer();

            Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();

            dbLayer.DataBaseName = Path.Combine(GlobalSettings.DataPath,c_DBName);

            m_DBLayer = dbLayer;

            m_serverTransportDBLayer = new Mimoza.DBLayer.Transport.Server.TransportDBLayer();

            m_hostRegistration = new ServiceHostRegistration(this);
            m_hostRegistration.Init();

            m_hostsConnInitiator = new HostsConnectionInitiator(this,
                new HostsConnections(this));
            m_hostsConnInitiator.Init();

            m_policyManager = new ServicePolicyManager(this);
            m_hostController = new ServiceHostController(this);

            m_updatesFileRepositiry = new UpdatesFileRepository(this);
        }

        public void DatabaseConnect()
        {
            m_DBLayer.Connect();
            m_objectLayer = new Mimoza.ObjectLayer.OL(m_DBLayer);
            m_serverTransportDBLayer.SetDBLayer(m_DBLayer);

            m_policyManager.Init();
            m_hostController.Init();
        }

        public void Deinit()
        {
            DestroyListeners();

            m_hostsConnInitiator.Shutdown();
            m_hostRegistration.Shutdown();
            m_DBLayer.Disconnect();

            m_policyManager.Shutdown();
            m_hostController.Shutdown();
        }

        public void CreateListener()
        {
            m_serviceUDPConnection = Mimoza.Transport.Transport.CreateServerUDPConnection(
                Settings.BroadcastTransportPort);
            Mimoza.Transport.Transport.Dispatcher.AddConnection(
                m_serviceUDPConnection);
            
            m_serviceListener = Mimoza.Transport.Transport.CreateListener(
                Settings.ServerTransportPort);
            Mimoza.Transport.Transport.Dispatcher.AddListener(
                m_serviceListener);
        }

        public void DestroyListeners()
        {
            if (m_serviceUDPConnection != null)
            {
                Mimoza.Transport.Transport.Dispatcher.RemoveConnection(
                    m_serviceUDPConnection);
            }

            if (m_serviceListener != null)
            {
                Mimoza.Transport.Transport.Dispatcher.RemoveListener(
                    m_serviceListener);
            }
        }

        public HostsConnectionInitiator HostsConnectionInitiator
        {
            get { return m_hostsConnInitiator; }
        }

        public ServicePolicyManager ServicePolicyManager
        {
            get { return m_policyManager; }
        }

        public ObjectLayer.OL ObjectLayer
        {
            get { return m_objectLayer; }
        }

        Settings m_settings;

        Mimoza.Common.IDBLayer m_DBLayer;
        Mimoza.DBLayer.Transport.Server.TransportDBLayer m_serverTransportDBLayer;
        ObjectLayer.OL m_objectLayer;

        Connection m_serviceUDPConnection;
        Listener m_serviceListener;
        
        ServiceHostRegistration m_hostRegistration;
        HostsConnectionInitiator m_hostsConnInitiator;

        ServicePolicyManager m_policyManager;
        ServiceHostController m_hostController;

        UpdatesFileRepository m_updatesFileRepositiry;
    }
}
